<template>
    <div>
        <my-nvaigation-bar></my-nvaigation-bar>
        <div id="Glxt_containner">
            <el-row>
                <el-col :span="24" id="Glxt_bigtitle">植物分类homeFamily</el-col>
            </el-row>
            
            
            <el-row style="margin-top: 20px;">
                <el-col :span="4" offset="4"><el-button type="primary" icon="el-icon-circle-plus-outline" @click="addType()">添加植物分类</el-button></el-col>
                <el-col :span="10">
                    <!--查询表单-->
                    <el-form :inline="true" class="demo-form-inline">
                    <el-form-item label="ID">
                        <el-input v-model="searchObj.id" placeholder="植物分类的ID" /> 
                    </el-form-item>
                    <el-form-item label="植物分类">
                        <el-input v-model="searchObj.familyName" placeholder="植物分类的名称" /> 
                    </el-form-item>
                    <el-button type="primary" icon="el-icon-search" @click="fetchData()">查询</el-button>
                    <el-button type="default" @click="resetData()">清空</el-button>
                    </el-form>
                </el-col>
            </el-row>
            
            
            <!--表格-->
            <el-table :data="list" style="width: 100%">
                <el-table-column prop="id" label="ID"></el-table-column>
                <el-table-column prop="familyName" label="植物分类"></el-table-column>
                <el-table-column prop="createTime" label="创建时间"></el-table-column>
                <el-table-column prop="updateTime" label="修改时间"></el-table-column>
                <el-table-column label="操作">
                    <template slot-scope="scope">
                        <el-button @click="editClick(scope.row.id)" type="text" size="small">编辑</el-button>
                        <el-button type="text" size="small" @click="deleteClick(scope.row.id)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <!--页码-->
            <el-row>
                <el-col :span="24"><el-pagination background layout="prev, pager, next" :total="total" :current-page="page" @current-change="changeCurrentPage"></el-pagination></el-col>
            </el-row>

            <!--编辑-->
            <el-drawer
            title="编辑植物分类信息"
            :visible.sync="drawer"
            with-header=true
            :before-close="handleClose">
            <div style="padding:30px">
                <el-form ref="editFamily" :model="form" label-width="60px">
                    <el-form-item label="ID">
                        <el-input v-model="editFamily.id" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="植物分类">
                        <el-input v-model="editFamily.familyName"></el-input>
                    </el-form-item>
                    <div style="margin-top: 10px;">
                        <el-button type="primary" @click="onSubmit">完成</el-button>
                    </div>                    
                </el-form>
            </div>
            </el-drawer>

            <!--添加-->
            <el-drawer
            title="添加植物分类信息"
            :visible.sync="drawerWithAdd"
            with-header=true
            :before-close="handleCloseWithAdd">
            <div style="padding:30px">
                <el-form ref="saveObj" :model="form" label-width="60px">
                    <el-form-item label="植物分类">
                        <el-input v-model="saveObj.familyName"></el-input>
                    </el-form-item>
                    <div style="margin-top: 10px;">
                        <el-button type="primary" @click="onSubmitWithSave">完成</el-button>
                    </div>                    
                </el-form>
            </div>
            </el-drawer>
        </div>
    </div>
</template>

<script>
import MyNvaigationBar from '../../components/MyNvaigationBar.vue';
import HomeFamilyApi from '../../api/home/homeFamily'

export default {
    components: { MyNvaigationBar },
    data() {
        return {
            list: [], // family列表
            total: 0, // 总记录数
            page: 1, // 页码
            limit: 10, // 每页记录数
            searchObj: {}, // 查询条件
            saveObj: {},//新增obj
            drawer: false, //编辑窗口
            drawerWithAdd: false, //添加窗口
            editFamily: {} //修改信息的内容
        }
    },
    methods: {
        //添加新的family
        onSubmitWithSave() {
        this.$confirm('你确定要添加新的植物分类吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                return HomeFamilyApi.addFamily(this.saveObj)
            }).then((res) => {
                if (res.data.code == 200) {
                    this.$message.success(res.data.message)
                    this.fetchData()
                    this.drawerWithAdd = false
                    this.saveObj = {}
                } else {
                    this.$message.error(res.data.message);
                }
            }).catch(err => {
                if (err === 'cancel') {
                    this.$message.info('取消添加')
                }
            })
        },
        //修改信息
        onSubmit() {
            this.$confirm('你确定要更改植物分类信息吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                return HomeFamilyApi.updateFamily(this.editFamily)
            }).then((res) => {
                if (res.data.code == 200) {
                    this.$message.success(res.data.message)
                    this.fetchData()
                    this.drawer = false
                } else {
                    this.$message.error(res.data.message);
                }
            }).catch(err => {
                if (err === 'cancel') {
                    this.$message.info('取消编辑')
                }
            })
        },
        //删除分类
        deleteClick(id) {
            this.$confirm('你确定要删除'+id+'号植物分类吗','提示',{
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                return HomeFamilyApi.remove(id)
            }).then((res) => {
                this.fetchData()
                this.$message.info(res.data.message)
            }).catch(err => {
                if (err === 'cancel') {
                    this.$message.info('取消删除')
                }
            })  
        },
        // 改变页码，page：回调参数，表示当前选中的“页码”
        changeCurrentPage(page) {
            this.page = page
            this.fetchData()
        },
        //添加
        addType() {
            this.drawerWithAdd = true;
        },
        //编辑信息
        editClick(id) {
            this.drawer = true;
            HomeFamilyApi.getFamilyById(id)
            .then(res => {
                this.editFamily = res.data.data
            })
        },
        // 重置表单
        resetData() {
            this.searchObj = {}
            this.fetchData()
        },
        //编辑信息关闭提醒
        handleClose(done) {
            this.$confirm('确定退出编辑植物分类信息？')
            .then(_ => {
                done();
            })
            .catch(_ => {});
        },
        //编辑信息关闭提醒（添加）
        handleCloseWithAdd(done) {
        this.$confirm('确定退出添加植物分类信息？')
            .then(_ => {
                this.saveObj = {}
                done();
            })
            .catch(_ => {});
        },
        //数据初始化
        fetchData() {
            HomeFamilyApi.pageList(this.page, this.limit, this.searchObj)
            .then(res => {
                this.list = res.data.data.records
                this.total = res.data.data.total
            })
        },
    },
    created() {
        this.fetchData()
    }
}

</script>

<style>
    #Glxt_containner{
        width: auto;
        height: auto;
        border:3px dashed #42b983;
        padding-top: 8px;
        padding-bottom: 8px;
        border-radius: 8px;
        margin-top: 10px;
    }

    #Glxt_bigtitle {
    font-family: Avenir, Helvetica, Arial, sans-serif;
    font-size: 20px;
    font-weight: bolder;
    color:#42b983;
    }

</style>